package com.ruoyi.stat.toilet.logic;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.erecord.region.domain.bo.RegionBo;
import com.ruoyi.erecord.region.domain.vo.RegionVo;
import com.ruoyi.erecord.region.service.IRegionService;
import com.ruoyi.standingbook.v2toilet.domain.vo.V2ToiletVo;
import com.ruoyi.stat.toilet.domain.bo.CommunityToiletStatBo;
import com.ruoyi.stat.toilet.domain.bo.V2ToiletBillStatBo;
import com.ruoyi.stat.toilet.domain.vo.CommunityToiletStatVo;
import com.ruoyi.stat.toilet.domain.vo.InspectionToiletStat;
import com.ruoyi.stat.toilet.domain.vo.ToiletCleanImgVo;
import com.ruoyi.stat.toilet.domain.vo.V2ToiletBillStatVo;
import com.ruoyi.stat.toilet.service.ICommunityToiletStatService;
import com.ruoyi.stat.toilet.service.IV2ToiletBillStatService;
import com.ruoyi.system.service.ISysConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 电子档案公厕数据统计逻辑Logic
 *
 * @author zf
 * @date 2023-06-08
 */
@RequiredArgsConstructor
@Service
public class CommunityToiletStatLogic {

    private final IRegionService iRegionService;
    private final ICommunityToiletStatService iCommunityToiletStatService;
    private final IV2ToiletBillStatService iV2ToiletBillService;
    private final ISysConfigService configService;

    /**
     * 获取数据
     * 1.monthNum数量的月份
     * 2.街道及街道下的社区
     *
     * @param monthNum 月份数量
     */
    public JSONObject getStreetsAndMonths(boolean all, Integer monthNum) {

        //获取monthNum数量的月份
        List<String> months = new ArrayList<>();
        for (int i = 0; i < monthNum; i++) {
            Date date = DateUtils.addMonth(new Date(), -1 * i);
            String strDate = DateUtils.dateTime(date);
            months.add(strDate.substring(0, 7));
            System.out.println("=================" + strDate);
        }

        //获取街道及街道下的社区
        List<Tree<String>> regions = iRegionService.treeListByGrade(4);

        JSONObject json = new JSONObject();
        if (all)
            json.set("months", months);
        json.set("regions", regions);
        return json;
    }

    /**
     * 获取街道或社区 公厕数量
     *
     * @param id   街道或社区 Id
     * @param type 类型 街道:1 或 社区:2
     */
    public List<CommunityToiletStatVo> getCommunityToiletStatNum(String id, String type) {
        List<String> communities = getCommunitis(id, type);
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        communityToiletStatBo.setCommunities(communities);
        return iCommunityToiletStatService.getCommunityToiletStatNum(communityToiletStatBo);
    }

    private List<String> getCommunitis(String id, String type) {
        List<String> communities = new ArrayList<>();
        if (type.equals("1")) {
            RegionBo region = new RegionBo();
            region.setParentId(id);
            List<RegionVo> regions = iRegionService.queryList(region);
            for (RegionVo item : regions) {
                communities.add(item.getTitle());
            }
        } else {
            String title = iRegionService.getRegionNameById(id);
            communities.add(title);
        }
        return communities;
    }

    /**
     * 获取街道或社区 日期统计
     *
     * @param id        街道或社区 Id
     * @param type      类型 街道:1 或 社区:2
     * @param queryDate 指定日期 {yyyy-dd-mm}
     */
    public JSONObject getCommunityToiletStatDate(String id, String type, Date queryDate) {
        List<String> communities = getCommunitis(id, type);
        JSONObject result = new JSONObject();

        //获取社区公厕 统计数据
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        communityToiletStatBo.setCommunities(communities);
        communityToiletStatBo.setQueryDate(queryDate);
        CommunityToiletStatVo communityToiletStatVo = iCommunityToiletStatService.getCommunityToiletStatByDate(communityToiletStatBo);

        Integer _anomalyCount = 0;
        Integer _anomalyProcessCount = 0;
        Integer _personNum = 0;
        Integer _cleanCount = 0;
        if (communityToiletStatVo != null) {
            _anomalyCount = communityToiletStatVo.getAnomalyCount();
            _anomalyProcessCount = communityToiletStatVo.getAnomalyProcessCount();
            _personNum = communityToiletStatVo.getPersonNum();
            _cleanCount = communityToiletStatVo.getCleanCount();
        }

        JSONObject json2 = new JSONObject();
        json2.set("anomalyCount", _anomalyCount);
        json2.set("anomalyProcessCount", _anomalyProcessCount);
        result.set("status2", json2);

        String url = "/workspace/elec/inspection/inspectionStati";
        JSONObject json = new JSONObject();
        json.set("region_id", "138");
        if (!type.equals("1"))
            json.set("region_name", communities.get(0));
        else json.set("region_name", "");
        json.set("place_name", "");
        json.set("begin_date", DateUtils.parseDateToStr("yyyy-MM-dd", queryDate));
        json.set("end_date", "");
        json.set("rnd", Math.random());
        String params = json.toString();
        R<List<InspectionToiletStat>> r = HttpLogic.getList(configService, url, params, InspectionToiletStat.class);
        List<InspectionToiletStat> list = r.getData();
        int anomalyCount_XZ = 0;
        int anomalyProcessCount_XZ = 0;
        int anomalyWaitCount_XZ = 0;
        if (list != null) {
            for (InspectionToiletStat item : list) {
                if(item.getTickets_source()==2)
                    continue;
                if (item.getStatus() == 1)
                    anomalyWaitCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
                if (item.getStatus() == 2)
                    anomalyProcessCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
            }
            anomalyCount_XZ = anomalyWaitCount_XZ+anomalyProcessCount_XZ;
        }
        JSONObject json1 = new JSONObject();
        json1.set("anomalyCount", anomalyCount_XZ);
        json1.set("anomalyProcessCount", anomalyProcessCount_XZ);
        result.set("status1", json1);

        Integer anomalyCount = _anomalyCount + anomalyCount_XZ;
        Integer anomalyProcessCount = _anomalyProcessCount + anomalyProcessCount_XZ;
        result.set("anomalyCount", anomalyCount);
        result.set("anomalyProcessCount", anomalyProcessCount);
        BigDecimal intactRate = BigDecimal.valueOf(23l - anomalyCount * 1l).divide(BigDecimal.valueOf(23l), 2, RoundingMode.HALF_UP);
        BigDecimal anomalyRate = BigDecimal.valueOf(anomalyCount * 1l).divide(BigDecimal.valueOf(23l), 2, RoundingMode.HALF_UP);
        result.set("intactRate", intactRate);
        result.set("anomalyRate", anomalyRate);
        result.set("personNum", _personNum);
        result.set("cleanCount", _cleanCount);

        return result;
    }

    /**
     * 获取街道或社区 月统计
     *
     * @param id        街道或社区 Id
     * @param type      类型 街道:1 或 社区:2
     * @param queryDate 指定月份 {yyyy-dd}
     */
    public JSONObject getCommunityToiletStatMonth(String id, String type, Date queryDate) {
        List<String> communities = getCommunitis(id, type);
        JSONObject result = new JSONObject();

        //获取社区公厕 统计数据
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        communityToiletStatBo.setCommunities(communities);
        communityToiletStatBo.setQueryDate(queryDate);
        CommunityToiletStatVo communityToiletStatVo = iCommunityToiletStatService.getCommunityToiletStatByMonth(communityToiletStatBo);

        Integer _anomalyCount = 0;
        Integer _anomalyProcessCount = 0;
        Integer _personNum = 0;
        Integer _cleanCount = 0;
        if (communityToiletStatVo != null) {
            _anomalyCount = communityToiletStatVo.getAnomalyCount();
            _anomalyProcessCount = communityToiletStatVo.getAnomalyProcessCount();
            _personNum = communityToiletStatVo.getPersonNum();
            _cleanCount = communityToiletStatVo.getCleanCount();
        }

        JSONObject json2 = new JSONObject();
        json2.set("anomalyCount", _anomalyCount);
        json2.set("anomalyProcessCount", _anomalyProcessCount);
        result.set("status2", json2);

        String url = "/workspace/elec/inspection/inspectionStati";
        JSONObject json = new JSONObject();
        json.set("region_id", "138");
        if (!type.equals("1"))
            json.set("region_name", communities.get(0));
        else json.set("region_name", "");
        json.set("place_name", "");
        json.set("begin_date", DateUtils.getFirstDayOfMonth(DateUtils.parseDateToStr("yyyy-MM-dd", queryDate)));
        json.set("end_date", DateUtils.getLastDayOfMonth(DateUtils.parseDateToStr("yyyy-MM-dd", queryDate)));
        json.set("rnd", Math.random());

        String params = json.toString();
        R<List<InspectionToiletStat>> r = HttpLogic.getList(configService, url, params, InspectionToiletStat.class);
        List<InspectionToiletStat> list = r.getData();
        int anomalyCount_XZ = 0;
        int anomalyProcessCount_XZ = 0;
        int anomalyWaitCount_XZ = 0;
        if (list != null) {
            for (InspectionToiletStat item : list) {
                if(item.getTickets_source()==2)
                    continue;
                if (item.getStatus() == 1)
                    anomalyWaitCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
                if (item.getStatus() == 2)
                    anomalyProcessCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
            }
            anomalyCount_XZ=anomalyWaitCount_XZ+anomalyProcessCount_XZ;
        }
        JSONObject json1 = new JSONObject();
        json1.set("anomalyCount", anomalyCount_XZ);
        json1.set("anomalyProcessCount", anomalyProcessCount_XZ);
        result.set("status1", json1);

        // 异常数=电子档案异常数+智检异常数
        Integer anomalyCount = _anomalyCount + anomalyCount_XZ;
        Integer anomalyProcessCount = _anomalyProcessCount + anomalyProcessCount_XZ;
        result.set("anomalyCount", anomalyCount);
        result.set("anomalyProcessCount", anomalyProcessCount);


        int days = DateUtils.getDaysOfMonth(queryDate);
        int toiletNum = 0;
        List<CommunityToiletStatVo> toiletStats = getCommunityToiletStatNum(id, type);
        if (toiletStats != null) {
            for (CommunityToiletStatVo stat : toiletStats) {
                toiletNum += stat.getToiletNum();
            }
        }
        toiletNum = toiletNum == 0 ? 1 : toiletNum;
        //(23*公厕数*月天数-异常数)/23*公厕数*月天数
        long count = toiletNum * 23l * days;
        BigDecimal intactRate = BigDecimal.valueOf(count - anomalyCount * 1l).divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP);
        BigDecimal anomalyRate = BigDecimal.valueOf(anomalyCount * 1l).divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP);

        result.set("intactRate", intactRate);
        result.set("anomalyRate", anomalyRate);
        result.set("personNum", _personNum);
        result.set("cleanCount", _cleanCount);

        JSONObject resultPrv = getCommunityToiletStatPrvMonth(id, type, queryDate);

        float prvCleanCount = Float.parseFloat(resultPrv.getStr("cleanCount"));
        float cleanCountDiffer = prvCleanCount==0?0:((_cleanCount - prvCleanCount)/prvCleanCount);
        result.set("cleanCountDiffer", cleanCountDiffer);

        float prvPersonNum = Float.parseFloat(resultPrv.getStr("personNum"));
        float personNumDiffer = prvPersonNum==0?0:((_personNum - prvPersonNum)/prvPersonNum);
        result.set("personNumDiffer", personNumDiffer);

        result.set("intactRateDiffer", (intactRate.subtract(BigDecimal.valueOf(Float.valueOf(resultPrv.getStr("intactRate"))))));

        float status1PrvAnomaly = Float.parseFloat(resultPrv.getJSONObject("status1").getStr("anomalyCount"));
        float status1Anomaly = status1PrvAnomaly==0?0:((anomalyCount_XZ - status1PrvAnomaly)/status1PrvAnomaly);
        result.set("status1AnomalyRateDiffer",status1Anomaly);

        float status2PrvAnomaly = Float.parseFloat(resultPrv.getJSONObject("status2").getStr("anomalyCount"));
        float status2Anomaly = status2PrvAnomaly==0?0:((_anomalyCount-status2PrvAnomaly)/status2PrvAnomaly);
        result.set("status2AnomalyRateDiffer",status2Anomaly);

        // 环比计算公式：（本期-上期）/上期*100% 若上期为0则 "-"；
        //result.set("anomalyRateDiffer", (anomalyRate.subtract(BigDecimal.valueOf(Float.valueOf(resultPrv.getStr("anomalyRate"))))));
        float prvAnomalyCount = Float.parseFloat(resultPrv.getStr("anomalyCount"));
        float anomalyRateDiffer = prvAnomalyCount==0?0:((anomalyCount - prvAnomalyCount)/prvAnomalyCount);
        result.set("anomalyRateDiffer",anomalyRateDiffer);



        return result;
    }

    private JSONObject getCommunityToiletStatPrvMonth(String id, String type, Date queryDate) {
        List<String> communities = getCommunitis(id, type);
        JSONObject result = new JSONObject();
        queryDate = DateUtils.addMonth(queryDate, -1);
        //获取社区公厕 统计数据
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        communityToiletStatBo.setCommunities(communities);
        communityToiletStatBo.setQueryDate(queryDate);
        CommunityToiletStatVo communityToiletStatVo = iCommunityToiletStatService.getCommunityToiletStatByMonth(communityToiletStatBo);

        Integer _anomalyCount = 0;
        Integer _anomalyProcessCount = 0;
        Integer _personNum = 0;
        Integer _cleanCount = 0;
        if (communityToiletStatVo != null) {
            _anomalyCount = communityToiletStatVo.getAnomalyCount();
            _anomalyProcessCount = communityToiletStatVo.getAnomalyProcessCount();
            _personNum = communityToiletStatVo.getPersonNum();
            _cleanCount = communityToiletStatVo.getCleanCount();
        }
        JSONObject json2 = new JSONObject();
        json2.set("anomalyCount", _anomalyCount);
        json2.set("anomalyProcessCount", _anomalyProcessCount);
        result.set("status2", json2);

        String url = "/workspace/elec/inspection/inspectionStati";
        JSONObject json = new JSONObject();
        json.set("region_id", "138");
        if (!type.equals("1"))
            json.set("region_name", communities.get(0));
        else json.set("region_name", "");
        json.set("place_name", "");
        json.set("begin_date", DateUtils.getFirstDayOfMonth(DateUtils.parseDateToStr("yyyy-MM-dd", queryDate)));
        json.set("end_date", DateUtils.getLastDayOfMonth(DateUtils.parseDateToStr("yyyy-MM-dd", queryDate)));
        json.set("rnd", Math.random());
        String params = json.toString();
        R<List<InspectionToiletStat>> r = HttpLogic.getList(configService, url, params, InspectionToiletStat.class);
        List<InspectionToiletStat> list = r.getData();
        int anomalyCount_XZ = 0;
        int anomalyProcessCount_XZ = 0;
        int anomalyWaitCount_XZ = 0;
        if (list != null) {
            for (InspectionToiletStat item : list) {
                if(item.getTickets_source()==2)
                    continue;
                if (item.getStatus() == 1)
                    anomalyWaitCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
                if (item.getStatus() == 2)
                    anomalyProcessCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
            }
            anomalyCount_XZ=anomalyWaitCount_XZ+anomalyProcessCount_XZ;
        }
        JSONObject json1 = new JSONObject();
        json1.set("anomalyCount", anomalyCount_XZ);
        json1.set("anomalyProcessCount", anomalyProcessCount_XZ);
        result.set("status1", json1);

        // 异常数=电子档案异常数+智检异常数
        Integer anomalyCount = _anomalyCount + anomalyCount_XZ;
        Integer anomalyProcessCount = _anomalyProcessCount + anomalyProcessCount_XZ;
        result.set("anomalyCount", anomalyCount);
        result.set("anomalyProcessCount", anomalyProcessCount);


        int days = DateUtils.getDaysOfMonth(queryDate);
        int toiletNum = 0;
        List<CommunityToiletStatVo> toiletStats = getCommunityToiletStatNum(id, type);
        if (toiletStats != null) {
            for (CommunityToiletStatVo stat : toiletStats) {
                toiletNum += stat.getToiletNum();
            }
        }
        toiletNum = toiletNum == 0 ? 1 : toiletNum;
        // (23*公厕数*月天数-异常数)/23*公厕数*月天数
        long count = toiletNum * 23l * days;
        BigDecimal intactRate = BigDecimal.valueOf(count - anomalyCount * 1l).divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP);
        BigDecimal anomalyRate = BigDecimal.valueOf(anomalyCount * 1l).divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP);
        result.set("intactRate", intactRate);
        result.set("anomalyRate", anomalyRate);
        result.set("personNum", _personNum);
        result.set("cleanCount", _cleanCount);

        return result;
    }

    /**
     * 获取公厕 日期统计
     *
     * @param toiletName 公厕名称
     * @param queryDate  指定日期 {yyyy-dd-mm}
     */
    public JSONObject getToiletStatDate(String toiletName, Date queryDate) {
        JSONObject result = new JSONObject();

        //获取社区公厕 统计数据
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        communityToiletStatBo.setToiletName(toiletName);
        communityToiletStatBo.setQueryDate(queryDate);
        CommunityToiletStatVo communityToiletStatVo = iCommunityToiletStatService.getToiletStatByDate(communityToiletStatBo);

        Integer _anomalyCount = 0;
        Integer _anomalyProcessCount = 0;
        Integer _personNum = 0;
        Integer _cleanCount = 0;
        if (communityToiletStatVo != null) {
            _anomalyCount = communityToiletStatVo.getAnomalyCount();
            _anomalyProcessCount = communityToiletStatVo.getAnomalyProcessCount();
            _personNum = communityToiletStatVo.getPersonNum();
            _cleanCount = communityToiletStatVo.getCleanCount();
        }

        JSONObject json2 = new JSONObject();
        json2.set("anomalyCount", _anomalyCount);
        json2.set("anomalyProcessCount", _anomalyProcessCount);
        result.set("status2", json2);

        String url = "/workspace/elec/inspection/inspectionStati";
        JSONObject json = new JSONObject();
        json.set("region_id", "138");
        json.set("region_name", "");
        json.set("place_name", toiletName);
        json.set("begin_date", DateUtils.parseDateToStr("yyyy-MM-dd", queryDate));
        json.set("end_date", "");
        json.set("rnd", Math.random());
        String params = json.toString();
        R<List<InspectionToiletStat>> r = HttpLogic.getList(configService, url, params, InspectionToiletStat.class);
        List<InspectionToiletStat> list = r.getData();
        int anomalyCount_XZ = 0;
        int anomalyProcessCount_XZ = 0;
        int anomalyWaitCount_XZ = 0;
        if (list != null) {
            for (InspectionToiletStat item : list) {
                if(item.getTickets_source()==2)
                    continue;
                if (item.getStatus() == 1)
                    anomalyWaitCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
                if (item.getStatus() == 2)
                    anomalyProcessCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
            }
            anomalyCount_XZ=anomalyWaitCount_XZ+anomalyProcessCount_XZ;
        }
        JSONObject json1 = new JSONObject();
        json1.set("anomalyCount", anomalyCount_XZ);
        json1.set("anomalyProcessCount", anomalyProcessCount_XZ);
        result.set("status1", json1);

        Integer anomalyCount = _anomalyCount + anomalyCount_XZ;
        Integer anomalyProcessCount = _anomalyProcessCount + anomalyProcessCount_XZ;
        result.set("anomalyCount", anomalyCount);
        result.set("anomalyProcessCount", anomalyProcessCount);
        BigDecimal intactRate = BigDecimal.valueOf(23l - anomalyCount * 1l).divide(BigDecimal.valueOf(23l), 2, RoundingMode.HALF_UP);
        BigDecimal anomalyRate = BigDecimal.valueOf(anomalyCount * 1l).divide(BigDecimal.valueOf(23l), 2, RoundingMode.HALF_UP);
        result.set("intactRate", intactRate);
        result.set("anomalyRate", anomalyRate);
        result.set("personNum", _personNum);
        result.set("cleanCount", _cleanCount);

        return result;
    }

    /**
     * 获取街道或社区 月统计
     *
     * @param toiletName 公厕名称
     * @param queryDate  指定月份 {yyyy-dd}
     */
    public JSONObject getToiletStatMonth(String toiletName, Date queryDate) {
        JSONObject result = new JSONObject();

        //获取社区公厕 统计数据
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        communityToiletStatBo.setToiletName(toiletName);
        communityToiletStatBo.setQueryDate(queryDate);
        CommunityToiletStatVo communityToiletStatVo = iCommunityToiletStatService.getToiletStatByMonth(communityToiletStatBo);

        Integer _anomalyCount = 0;
        Integer _anomalyProcessCount = 0;
        Integer _personNum = 0;
        Integer _cleanCount = 0;
        if (communityToiletStatVo != null) {
            _anomalyCount = communityToiletStatVo.getAnomalyCount();
            _anomalyProcessCount = communityToiletStatVo.getAnomalyProcessCount();
            _personNum = communityToiletStatVo.getPersonNum();
            _cleanCount = communityToiletStatVo.getCleanCount();
        }

        JSONObject json2 = new JSONObject();
        json2.set("anomalyCount", _anomalyCount);
        json2.set("anomalyProcessCount", _anomalyProcessCount);
        result.set("status2", json2);

        String url = "/workspace/elec/inspection/inspectionStati";
        JSONObject json = new JSONObject();
        json.set("region_id", "138");
        json.set("region_name", "");
        json.set("place_name", toiletName);
        json.set("begin_date", DateUtils.getFirstDayOfMonth(DateUtils.parseDateToStr("yyyy-MM-dd", queryDate)));
        json.set("end_date", DateUtils.getLastDayOfMonth(DateUtils.parseDateToStr("yyyy-MM-dd", queryDate)));
        json.set("rnd", Math.random());
        String params = json.toString();
        R<List<InspectionToiletStat>> r = HttpLogic.getList(configService, url, params, InspectionToiletStat.class);
        List<InspectionToiletStat> list = r.getData();
        int anomalyCount_XZ = 0;
        int anomalyProcessCount_XZ = 0;
        int anomalyWaitCount_XZ = 0;
        if (list != null) {
            for (InspectionToiletStat item : list) {
                if(item.getTickets_source()==2)
                    continue;
                if (item.getStatus() == 1)
                    anomalyWaitCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
                if (item.getStatus() == 2)
                    anomalyProcessCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
            }
            anomalyCount_XZ=anomalyWaitCount_XZ+anomalyProcessCount_XZ;
        }
        JSONObject json1 = new JSONObject();
        json1.set("anomalyCount", anomalyCount_XZ);
        json1.set("anomalyProcessCount", anomalyProcessCount_XZ);
        result.set("status1", json1);

        // 异常数=电子档案异常数+智检异常数
        Integer anomalyCount = _anomalyCount + anomalyCount_XZ;
        Integer anomalyProcessCount = _anomalyProcessCount + anomalyProcessCount_XZ;
        result.set("anomalyCount", anomalyCount);
        result.set("anomalyProcessCount", anomalyProcessCount);


        int days = DateUtils.getDaysOfMonth(queryDate);
        int toiletNum = 1;
        // (23*公厕数*月天数-异常数)/23*公厕数*月天数
        long count = toiletNum * 23l * days;
        BigDecimal intactRate = BigDecimal.valueOf(count - anomalyCount * 1l).divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP);
        BigDecimal anomalyRate = BigDecimal.valueOf(anomalyCount * 1l).divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP);
        result.set("intactRate", intactRate);
        result.set("anomalyRate", anomalyRate);
        result.set("personNum", _personNum);
        result.set("cleanCount", _cleanCount);

        JSONObject resultPrv = getToiletStatPrvMonth(toiletName, queryDate);

        float prvCleanCount = Float.parseFloat(resultPrv.getStr("cleanCount"));
        float cleanCountDiffer = prvCleanCount==0?0:((_cleanCount - prvCleanCount)/prvCleanCount);
        result.set("cleanCountDiffer", cleanCountDiffer);

        float prvPersonNum = Float.parseFloat(resultPrv.getStr("personNum"));
        float personNumDiffer = prvPersonNum==0?0:((_personNum - prvPersonNum)/prvPersonNum);
        result.set("personNumDiffer", personNumDiffer);

//        result.set("cleanCountDiffer", (_cleanCount - Integer.valueOf(resultPrv.getStr("cleanCount"))));
//        result.set("personNumDiffer", (_personNum - Integer.valueOf(resultPrv.getStr("personNum"))));
        result.set("intactRateDiffer", (intactRate.subtract(BigDecimal.valueOf(Float.valueOf(resultPrv.getStr("intactRate"))))));
//        result.set("anomalyRateDiffer", (anomalyRate.subtract(BigDecimal.valueOf(Float.valueOf(resultPrv.getStr("anomalyRate"))))));

        float status1PrvAnomaly = Float.parseFloat(resultPrv.getJSONObject("status1").getStr("anomalyCount"));
        float status1Anomaly = status1PrvAnomaly==0?0:((anomalyCount_XZ - status1PrvAnomaly)/status1PrvAnomaly);
        result.set("status1AnomalyRateDiffer",status1Anomaly);

        float status2PrvAnomaly = Float.parseFloat(resultPrv.getJSONObject("status2").getStr("anomalyCount"));
        float status2Anomaly = status2PrvAnomaly==0?0:((_anomalyCount-status2PrvAnomaly)/status2PrvAnomaly);
        result.set("status2AnomalyRateDiffer",status2Anomaly);

        float prvAnomalyCount = Float.parseFloat(resultPrv.getStr("anomalyCount"));
        float anomalyRateDiffer = prvAnomalyCount==0?0:((anomalyCount - prvAnomalyCount)/prvAnomalyCount);
        result.set("anomalyRateDiffer",anomalyRateDiffer);

        return result;
    }

    private JSONObject getToiletStatPrvMonth(String toiletName, Date queryDate) {
        JSONObject result = new JSONObject();
        queryDate = DateUtils.addMonth(queryDate, -1);
        //获取社区公厕 统计数据
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        communityToiletStatBo.setToiletName(toiletName);
        communityToiletStatBo.setQueryDate(queryDate);
        CommunityToiletStatVo communityToiletStatVo = iCommunityToiletStatService.getToiletStatByMonth(communityToiletStatBo);

        Integer _anomalyCount = 0;
        Integer _anomalyProcessCount = 0;
        Integer _personNum = 0;
        Integer _cleanCount = 0;
        if (communityToiletStatVo != null) {
            _anomalyCount = communityToiletStatVo.getAnomalyCount();
            _anomalyProcessCount = communityToiletStatVo.getAnomalyProcessCount();
            _personNum = communityToiletStatVo.getPersonNum();
            _cleanCount = communityToiletStatVo.getCleanCount();
        }
        JSONObject json2 = new JSONObject();
        json2.set("anomalyCount", _anomalyCount);
        json2.set("anomalyProcessCount", _anomalyProcessCount);
        result.set("status2", json2);

        String url = "/workspace/elec/inspection/inspectionStati";
        JSONObject json = new JSONObject();
        json.set("region_id", "138");
        json.set("region_name", "");
        json.set("place_name", toiletName);
        json.set("begin_date", DateUtils.getFirstDayOfMonth(DateUtils.parseDateToStr("yyyy-MM-dd", queryDate)));
        json.set("end_date", DateUtils.getLastDayOfMonth(DateUtils.parseDateToStr("yyyy-MM-dd", queryDate)));
        json.set("rnd", Math.random());
        String params = json.toString();
        R<List<InspectionToiletStat>> r = HttpLogic.getList(configService, url, params, InspectionToiletStat.class);
        List<InspectionToiletStat> list = r.getData();
        int anomalyCount_XZ = 0;
        int anomalyProcessCount_XZ = 0;
        int anomalyWaitCount_XZ = 0;
        if (list != null) {
            for (InspectionToiletStat item : list) {
                if(item.getTickets_source()==2)
                    continue;
                if (item.getStatus() == 1)
                    anomalyWaitCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
                if (item.getStatus() == 2)
                    anomalyProcessCount_XZ = item.getCnt() == null ? 0 : item.getCnt();
            }
            anomalyCount_XZ=anomalyWaitCount_XZ+anomalyProcessCount_XZ;
        }
        JSONObject json1 = new JSONObject();
        json1.set("anomalyCount", anomalyCount_XZ);
        json1.set("anomalyProcessCount", anomalyProcessCount_XZ);
        result.set("status1", json1);

        // 异常数=电子档案异常数+智检异常数
        Integer anomalyCount = _anomalyCount + anomalyCount_XZ;
        Integer anomalyProcessCount = _anomalyProcessCount + anomalyProcessCount_XZ;
        result.set("anomalyCount", anomalyCount);
        result.set("anomalyProcessCount", anomalyProcessCount);


        int days = DateUtils.getDaysOfMonth(queryDate);
        int toiletNum = 1;
        // (23*公厕数*月天数-异常数)/23*公厕数*月天数
        long count = toiletNum * 23l * days;
        BigDecimal intactRate = BigDecimal.valueOf(count - anomalyCount * 1l).divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP);
        BigDecimal anomalyRate = BigDecimal.valueOf(anomalyCount * 1l).divide(BigDecimal.valueOf(count), 2, RoundingMode.HALF_UP);
        result.set("intactRate", intactRate);
        result.set("anomalyRate", anomalyRate);
        result.set("personNum", _personNum);
        result.set("cleanCount", _cleanCount);

        return result;
    }


    /**
     * 获取数据
     * 1.dateNum数量的日期
     * 2.街道及街道下的社区
     *
     * @param dateNum 日期数量
     */
    public JSONObject getStreetsAndDates(Integer dateNum) {

        //获取monthNum数量的月份
        List<String> dates = new ArrayList<>();
        for (int i = 0; i < dateNum; i++) {
            Date date = DateUtils.addDay(new Date(), -1 * i);
            String strDate = DateUtils.dateTime(date);
            dates.add(strDate.substring(0, 10));
            System.out.println("=================" + strDate);
        }

        //获取街道及街道下的社区
        List<Tree<String>> regions = iRegionService.treeListByGrade(4);

        JSONObject json = new JSONObject();
        json.set("dates", dates);
        json.set("regions", regions);
        return json;
    }

    /**
     * 获取街道或社区 整月 月统计
     *
     * @param toiletName 公厕名称
     * @param queryDate  指定月份 {yyyy-dd}
     */
    public JSONObject getCommunityToiletStatMonthAndMaxMin(String toiletName, Date queryDate) {
        //List<String> communities = getCommunitis(id,type);
        JSONObject result = new JSONObject();

        String val = "1";//异常;
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        //communityToiletStatBo.setCommunities(communities);
        communityToiletStatBo.setToiletName(toiletName);
        communityToiletStatBo.setQueryDate(queryDate);
        communityToiletStatBo.setVal(val);
//        CommunityToiletStatVo communityToiletStatVoMax = iCommunityToiletStatService.getCommunityToiletStatMaxByMonth(communityToiletStatBo);
//
//        CommunityToiletStatVo communityToiletStatVoMin = iCommunityToiletStatService.getCommunityToiletStatMinByMonth(communityToiletStatBo);
//
//        Integer max=0;
//        String maxToiletName="";
//        if(communityToiletStatVoMax!=null){
//            max = communityToiletStatVoMax.getAnomalyCount();
//            maxToiletName=communityToiletStatVoMax.getToiletName();
//        }
//        Integer min=0;
//        String minToiletName="";
//        if(communityToiletStatVoMin!=null){
//            min = communityToiletStatVoMin.getAnomalyCount();
//            minToiletName=communityToiletStatVoMin.getToiletName();
//        }
//

        List<V2ToiletVo> list = iCommunityToiletStatService.getCommunityToiletStatEveryDateByMonth(communityToiletStatBo);

        Map<String, String> attrs = new LinkedHashMap<>();
        attrs.put("anomalyCount", "异常数量");
        attrs.put("anomalyProcessCount", "已处理数");
        attrs.put("opened", "公厕正常开放");
        attrs.put("logo", "公共洗手间标识");
        attrs.put("ventilate", "通风除臭");
        attrs.put("handBasin", "洗手盆(台)");
        attrs.put("handry", "擦手纸或干手机");
        attrs.put("handSanitizer", "洗手液");
        attrs.put("waterFaucet", "水龙头");
        attrs.put("masks", "面镜");
        attrs.put("floor", "地面");
        attrs.put("wallspace", "窗户、墙面");
        attrs.put("ceiling", "天花板");
        attrs.put("lights", "照明设施");
        attrs.put("innerClean", "厕内整洁");
        attrs.put("cubicle", "厕门、厕位隔断");
        attrs.put("cubicleTissue", "厕间纸巾");
        attrs.put("toilet", "大小便器");
        attrs.put("roomClean", "厕间干净");
        attrs.put("flushing", "冲水设备");
        attrs.put("grabar", "大便厕位安全扶手");
        attrs.put("hardware", "手纸架、挂钩、搁物板");
        attrs.put("childHandBasin", "儿童洗手盆");
        attrs.put("barrierfreeFacilities", "无障碍设施");
        attrs.put("babyCareRoom", "母婴室");
        attrs.put("cleanCount", "清洁频率(次)");

        Map<String, Map<String, String>> map = new HashMap<>();
        map.put("anomalyCount", new HashMap<>());
        map.put("anomalyProcessCount", new HashMap<>());
        map.put("opened", new HashMap<>());
        map.put("logo", new HashMap<>());
        map.put("ventilate", new HashMap<>());
        map.put("handBasin", new HashMap<>());
        map.put("handry", new HashMap<>());
        map.put("handSanitizer", new HashMap<>());
        map.put("waterFaucet", new HashMap<>());
        map.put("masks", new HashMap<>());
        map.put("floor", new HashMap<>());
        map.put("wallspace", new HashMap<>());
        map.put("ceiling", new HashMap<>());
        map.put("lights", new HashMap<>());
        map.put("innerClean", new HashMap<>());
        map.put("cubicle", new HashMap<>());
        map.put("cubicleTissue", new HashMap<>());
        map.put("toilet", new HashMap<>());
        map.put("roomClean", new HashMap<>());
        map.put("flushing", new HashMap<>());
        map.put("grabar", new HashMap<>());
        map.put("hardware", new HashMap<>());
        map.put("childHandBasin", new HashMap<>());
        map.put("barrierfreeFacilities", new HashMap<>());
        map.put("babyCareRoom", new HashMap<>());
        map.put("cleanCount", new HashMap<>());

        int days = DateUtils.getDaysOfMonth(queryDate);
        String month = DateUtils.parseDateToStr("yyyy-MM", queryDate);
        for (int i = 1; i <= days; i++) {
            String date = month + "-" + String.format("%02d", i);
            String key = "d" + i;
            Optional<V2ToiletVo> obj = list.stream().filter(m -> m.getCreateDate() != null && Objects.equals(DateUtils.parseDateToStr("yyyy-MM-dd", m.getCreateDate()), date)).findFirst();
            if (obj.isPresent()) {
                V2ToiletVo vo = obj.get();

                map.get("anomalyCount").put(key, vo.getAnomalyCount().toString());
                map.get("anomalyProcessCount").put(key, vo.getAnomalyProcessCount().toString());
                map.get("opened").put(key, vo.getOpened());
                map.get("logo").put(key, vo.getLogo());
                map.get("ventilate").put(key, vo.getVentilate());
                map.get("handBasin").put(key, vo.getHandBasin());
                map.get("handry").put(key, vo.getHandry());
                map.get("handSanitizer").put(key, vo.getHandSanitizer());
                map.get("waterFaucet").put(key, vo.getWaterFaucet());
                map.get("masks").put(key, vo.getMasks());
                map.get("floor").put(key, vo.getFloor());
                map.get("wallspace").put(key, vo.getWallspace());
                map.get("ceiling").put(key, vo.getCeiling());
                map.get("lights").put(key, vo.getLights());
                map.get("innerClean").put(key, vo.getInnerClean());
                map.get("cubicle").put(key, vo.getCubicle());
                map.get("cubicleTissue").put(key, vo.getCubicleTissue());
                map.get("toilet").put(key, vo.getToilet());
                map.get("roomClean").put(key, vo.getRoomClean());
                map.get("flushing").put(key, vo.getFlushing());
                map.get("grabar").put(key, vo.getGrabar());
                map.get("hardware").put(key, vo.getHardware());
                map.get("childHandBasin").put(key, vo.getChildHandBasin());
                map.get("barrierfreeFacilities").put(key, vo.getBarrierfreeFacilities());
                map.get("babyCareRoom").put(key, vo.getBabyCareRoom());
                map.get("cleanCount").put(key, vo.getCleanCount().toString());
            }
        }
        Map<String, Integer> rCount = new HashMap<>();

        int opened = map.get("opened").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("opened", opened);
        int logo = map.get("logo").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("logo", logo);
        int ventilate = map.get("ventilate").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("ventilate", ventilate);
        int handBasin = map.get("handBasin").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("handBasin", handBasin);
        int handry = map.get("handry").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("handry", handry);
        int handSanitizer = map.get("handSanitizer").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("handSanitizer", handSanitizer);
        int waterFaucet = map.get("waterFaucet").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("waterFaucet", waterFaucet);
        int masks = map.get("masks").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("masks", masks);
        int floor = map.get("floor").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("floor", floor);
        int wallspace = map.get("wallspace").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("wallspace", wallspace);
        int ceiling = map.get("ceiling").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("ceiling", ceiling);
        int lights = map.get("lights").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("lights", lights);
        int innerClean = map.get("innerClean").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("innerClean", innerClean);
        int cubicle = map.get("cubicle").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("cubicle", cubicle);
        int cubicleTissue = map.get("cubicleTissue").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("cubicleTissue", cubicleTissue);
        int toilet = map.get("toilet").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("toilet", toilet);
        int roomClean = map.get("roomClean").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("roomClean", roomClean);
        int flushing = map.get("flushing").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("flushing", flushing);
        int grabar = map.get("grabar").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("grabar", grabar);
        int hardware = map.get("hardware").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("hardware", hardware);
        int childHandBasin = map.get("childHandBasin").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("childHandBasin", childHandBasin);
        int barrierfreeFacilities = map.get("barrierfreeFacilities").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("barrierfreeFacilities", barrierfreeFacilities);
        int babyCareRoom = map.get("babyCareRoom").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> v.getValue().equals(val) ? 1 : 0).sum();
        rCount.put("babyCareRoom", babyCareRoom);





        Optional<Integer> maxobj = rCount.values().stream().max(Comparator.comparing(Integer::intValue));
        Map<String, Integer> maxlist = new HashMap<>();
        if (maxobj.isPresent()) {
            Integer max = maxobj.get().intValue();
            List<Map.Entry<String, Integer>> maxs = rCount.entrySet().stream().filter(f -> f.getValue().intValue() == max).collect(Collectors.toList());
            for (Map.Entry<String, Integer> entry : maxs) {
                maxlist.put(entry.getKey(), max);
            }
            result.set("max", maxlist);
        }

        Optional<Integer> minobj = rCount.values().stream().min(Comparator.comparing(Integer::intValue));
        Map<String, Integer> minlist = new HashMap<>();
        if (minobj.isPresent()) {
            Integer min = minobj.get().intValue();
            List<Map.Entry<String, Integer>> mins = rCount.entrySet().stream().filter(f -> f.getValue().intValue() == min).collect(Collectors.toList());
            for (Map.Entry<String, Integer> entry : mins) {
                minlist.put(entry.getKey(), min);
            }
            result.set("min", minlist);
        }

        int cleanCount = map.get("cleanCount").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> Integer.valueOf(v.getValue())).sum();
        rCount.put("cleanCount", cleanCount);
        int anomalyCount = map.get("anomalyCount").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> Integer.valueOf(v.getValue())).sum();
        rCount.put("anomalyCount", anomalyCount);
        int anomalyProcessCount = map.get("anomalyProcessCount").entrySet().stream().filter(m -> m.getKey().contains("d")).mapToInt(v -> Integer.valueOf(v.getValue())).sum();
        rCount.put("anomalyProcessCount", anomalyProcessCount);

        result.set("list", map);
        result.set("rc", rCount);
        result.set("days", days);
        result.set("columns", attrs);
        return result;
    }

    /**
     * 获取街道或社区 七日 日期统计
     *
     * @param id        街道或社区 Id
     * @param type      类型 街道:1 或 社区:2
     * @param queryDate 指定日期 {yyyy-dd-mm}
     */
    public JSONObject getCommunityToiletStatDateAndMaxMin(String id, String type, Date queryDate) {
        List<String> communities = getCommunitis(id, type);
        String val = "1";//异常
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        communityToiletStatBo.setCommunities(communities);
        communityToiletStatBo.setQueryDate(queryDate);
        communityToiletStatBo.setVal(val);
        List<CommunityToiletStatVo> maxs = iCommunityToiletStatService.getCommunityToiletStatMaxByDate(communityToiletStatBo);

        List<CommunityToiletStatVo> mins = iCommunityToiletStatService.getCommunityToiletStatMinByDate(communityToiletStatBo);

        JSONObject result = getCommunityToiletStatDate(id, type, queryDate);
        result.set("max", maxs);
        result.set("min", mins);

        List<V2ToiletVo> list = iCommunityToiletStatService.getCommunityToiletStatEveryDateByDate(communityToiletStatBo);
        result.set("list", list);
//        Optional<V2ToiletVo> find = list.stream().filter(f->{return f.getIdx()==-10;}).findFirst();
//        if(find.isPresent()) {
//            V2ToiletVo obj = find.get();
//            result.set("anomalyCount",obj.getAnomalyCount());
//            result.set("anomalyProcessCount", obj.getAnomalyProcessCount());
//        }
        Map<String, String> attrs = new LinkedHashMap<>();
//        attrs.put("toiletName","公厕名称");
//        attrs.put("ower","负责人");
//        attrs.put("phoneNo","联系电话");
        attrs.put("anomalyCount", "异常数量");
        attrs.put("anomalyProcessCount", "已处理数");
        attrs.put("opened", "公厕正常开放");
        attrs.put("logo", "公共洗手间标识");
        attrs.put("ventilate", "通风除臭");
        attrs.put("handBasin", "洗手盆(台)");
        attrs.put("handry", "擦手纸或干手机");
        attrs.put("handSanitizer", "洗手液");
        attrs.put("waterFaucet", "水龙头");
        attrs.put("masks", "面镜");
        attrs.put("floor", "地面");
        attrs.put("wallspace", "窗户、墙面");
        attrs.put("ceiling", "天花板");
        attrs.put("lights", "照明设施");
        attrs.put("innerClean", "厕内整洁");
        attrs.put("cubicle", "厕门、厕位隔断");
        attrs.put("cubicleTissue", "厕间纸巾");
        attrs.put("toilet", "大小便器");
        attrs.put("roomClean", "厕间干净");
        attrs.put("flushing", "冲水设备");
        attrs.put("grabar", "大便厕位安全扶手");
        attrs.put("hardware", "手纸架、挂钩、搁物板");
        attrs.put("childHandBasin", "儿童洗手盆");
        attrs.put("barrierfreeFacilities", "无障碍设施");
        attrs.put("babyCareRoom", "母婴室");
        attrs.put("cleanCount", "清洁频率(次)");
        result.set("columns", attrs);
        return result;
    }

    /**
     * 获取街道或社区 清洁图
     *
     * @param id        街道或社区 Id
     * @param type      类型 街道:1 或 社区:2
     * @param queryDate 指定日期 {yyyy-dd-mm}
     */
    public List<ToiletCleanImgVo> getCommunityToiletCleanImgs(String id, String type, Date queryDate) {
        List<String> communities = getCommunitis(id, type);
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        communityToiletStatBo.setQueryDate(queryDate);
        communityToiletStatBo.setCommunities(communities);
        List<V2ToiletBillStatVo> imgs = iV2ToiletBillService.queryToiletImgs(communityToiletStatBo);
        List<ToiletCleanImgVo> _list = new ArrayList<>();
        if (imgs.size() > 0) {
            for (V2ToiletBillStatVo item : imgs) {
                if (_list.size() == 8)
                    break;
                ToiletCleanImgVo vo = new ToiletCleanImgVo();
                if (item.getStatus() == 3)
                    vo.setType(2);
                else vo.setType(1);
                vo.setImg(item.getImageBefore());
                vo.setImg_a(item.getImageAfter());
                _list.add(vo);
            }
        } else {
            int num = imgs != null ? (8 - imgs.size()) : 8;

            String url = "/workspace/elec/inspection/inspectionImgs";
            JSONObject json = new JSONObject();
            json.set("region_id", "138");
            json.set("region_name", "");
            json.set("place_name", "");
            json.set("begin_date", "");
            json.set("end_date", "");
            json.set("rnd", Math.random());
            String params = json.toString();
            R<List<ToiletCleanImgVo>> r = HttpLogic.getList(configService, url, params, ToiletCleanImgVo.class);
            List<ToiletCleanImgVo> list = r.getData();
            if (list != null) {
                for (int i = 0; i < num; i++) {
                    _list.add(list.get(i));
                }
            }
        }
        return _list;
    }

    public List<V2ToiletBillStatVo> queryToiletBillByDate(String id, String type, Date queryDate) {
        List<String> communities = getCommunitis(id, type);
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        if (type.equals("3"))
            communityToiletStatBo.setToiletName(id);
        else communityToiletStatBo.setCommunities(communities);

        communityToiletStatBo.setQueryDate(queryDate);
        return iV2ToiletBillService.queryToiletBillByDate(communityToiletStatBo);
    }

    public List<V2ToiletBillStatVo> queryToiletBillByMonth(String id, String type, Date queryDate) {
        List<String> communities = getCommunitis(id, type);
        CommunityToiletStatBo communityToiletStatBo = new CommunityToiletStatBo();
        if (type.equals("3"))
            communityToiletStatBo.setToiletName(id);
        else communityToiletStatBo.setCommunities(communities);

        communityToiletStatBo.setQueryDate(queryDate);
        return iV2ToiletBillService.queryToiletBillByMonth(communityToiletStatBo);
    }

    public List<V2ToiletBillStatVo> getToiletBills(V2ToiletBillStatBo bo) {
        return iV2ToiletBillService.getToiletBills(bo);
    }

    public String getToiletBillCount(Date queryDate) {
        V2ToiletBillStatBo bo = new V2ToiletBillStatBo();
        bo.setStatus(0);
        bo.setInspectDate(queryDate);
        Integer count = iV2ToiletBillService.getToiletBillCount(bo);
        if (count == null || count == 0)
            return "-";
        count = count == null || count == 0 ? 1 : count;
        bo.setStatus(4);
        Integer over = iV2ToiletBillService.getToiletBillCount(bo);
        over = over == null||over==0 ? count : over;
        return String.valueOf(BigDecimal.valueOf(over * 1l).divide(BigDecimal.valueOf(count * 1l), 2, RoundingMode.HALF_UP));
    }
}
